//
//  CustomNavigationDelegate.m
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/5.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomNavigationDelegate.h"

@interface CustomNavigationDelegate()
@property (nonatomic, assign) UINavigationControllerOperation operation;
@end

@implementation CustomNavigationDelegate
- (instancetype)init {
    self = [super init];
    if (self) {
        _interactionController = [[UIPercentDrivenInteractiveTransition alloc] init];
    }
    return self;
}

#pragma mark - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    self.operation = operation;
    return self;
}

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
    return self.interactive ? self.interactionController : nil;
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    if (self.operation == UINavigationControllerOperationNone) {
        return;
    }

    UIView *containerView = transitionContext.containerView;
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    if (!fromVC || !toVC) {
        return;
    }
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    [containerView addSubview:toView];
    CGFloat translation = containerView.frame.size.height - 49 - 64;

    if (self.operation == UINavigationControllerOperationPush) {
        
        toView.transform = CGAffineTransformMakeTranslation(0, translation);

        [UIView animateWithDuration:[self transitionDuration: transitionContext] animations:^{
            fromView.transform = CGAffineTransformMakeTranslation(0, -translation);
            toView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            if (finished) {
                fromView.transform = CGAffineTransformIdentity;
                toView.transform = CGAffineTransformIdentity;
                [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            }
        }];
    } else {

        [containerView bringSubviewToFront:fromView];
        toView.transform = CGAffineTransformMakeTranslation(0, -translation);

        [UIView animateWithDuration:[self transitionDuration: transitionContext] animations:^{
            fromView.transform = CGAffineTransformMakeTranslation(0, translation);
            toView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            fromView.transform = CGAffineTransformIdentity;
            toView.transform = CGAffineTransformIdentity;
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
}

@end

